package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询套餐数据
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //调用数据库查询分页信息
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {
        //拷贝数据
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //操作数据库完成套餐表新增,同时获取到套餐的id
        setmealMapper.saveSetmeal(setmeal);
        Long setmealId=setmeal.getId();
        log.info("获取到套餐id:{}", setmealId);
        //循环遍历集合,拿到每一个菜品,将主键id和菜品绑定
        List<SetmealDish> setmealDishesList=setmealDTO.getSetmealDishes();
        setmealDishesList.forEach(setmealDish -> setmealDish.setSetmealId(setmealId)
        );
        //操作数据库,完成套餐表和菜品表的关联
        setmealDishMapper.saveSetmealWithDish(setmealDishesList);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */

    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        //起售中的套餐不能删除
        //删除套餐的同时也要删除套餐-菜品表中关联的数据

        //遍历集合,获取其中的每个套餐id
        ids.forEach(id -> {
            //判断套餐的状态是否是起售中
            String status=setmealMapper.selectSetmeal(id);
            if (status.equals("1")) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            //删除套餐
            setmealMapper.deleteSetmeal(id);
            //删除套餐关联的套餐_菜品表中数据
            setmealDishMapper.deleteSetmealWithDish(id);
        });
    }

    /**
     * 根据id查询套餐数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐数据
        SetmealVO setmealVO=setmealMapper.getById(id);
        //查询查询套餐_菜品表数据
        List<SetmealDish> setmealDishesList=setmealDishMapper.list(id);
        //将套餐-菜品表数据封装到套餐数据中,并且返回
        setmealVO.setSetmealDishes(setmealDishesList);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //拷贝数据
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //调用数据库,完成修改
        setmealMapper.updateSetmeal(setmeal);
        //获取套餐id,套餐_菜品集合
        Long id=setmeal.getId();
        List<SetmealDish> setmealDishesList=setmealDTO.getSetmealDishes();
        log.info("获取套餐id:{}", id);
        //删除原套餐id下的数据
        setmealDishMapper.deleteSetmealWithDish(id);
        //循环遍历集合,将套餐id插入其中
        setmealDishesList.forEach(setmealDish -> setmealDish.setSetmealId(id));
        //添加新的数据
        setmealDishMapper.saveSetmealWithDish(setmealDishesList);
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(String status, Long id) {
        //调用数据库,完成修改
        setmealMapper.updateStatus(status, id);
    }
}
